package com.zmc.dynamic.controller;

import com.alibaba.fastjson.JSON;
import com.zmc.dynamic.domain.User;
import com.zmc.dynamic.service.UserService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


@RestController
@RequestMapping("user/")
public class UserController {

    @Resource
    private UserService userService;

    @RequestMapping("save")
    public String save(){
        User user = new User();
        user.setNickName("阿三");
        user.setRealName("张三丰");
        user.setVersion(0);
        userService.save(user);
        return "ok";
    }

    @RequestMapping("update/{id}")
    public String update(@PathVariable("id") Long id){
        User user = userService.getByUserId(id);
        user.setRealName("66666");
        user.setNickName("666666");
        user.setVersion((int)Math.random() * 100);
        boolean left = userService.updateUser(user);
        return "ok";
    }

    @RequestMapping("get/{id}")
    public String get(@PathVariable("id") Long id){
        User byId = userService.getById(id);
        return JSON.toJSONString(byId);
    }


}
